const $api = require('./api.js').API;

function login(e){
  if(!wx.getStorageSync('uid')){
    e.setData({
      loginShow:true
    })
    //wxLogin(e);
  }else{
    checkphone(e);
  }
}

//查询手机号  是否注册
function checkphone(e){
  var data = {
    'uid':wx.getStorageSync('uid')
  }
  $api.post('member/phone',data).then(res =>{
    if(res.data.code == 1){
      
    }else{
      e.setData({
        phoneShow:true
      })
    }
  })
}
//微信登陆
function wxLogin(e,info){
  wx.login({
    success (res){
      var data = {'code':res.code}
      $api.get('getopenid',data).then(res => {
          info.openid = res.data.openid
          addUser(info,e)
      }).catch(err => {
        //请求失败
        //console.log('error','请求失败');
      })
    }
  })
}

//添加用户e
function addUser(info,e){
    var data ={
        'openId':info.openid,
        'nickName':info.nickName,
        'avatarUrl':info.avatarUrl,
        'puid':e.data.puid,
        'uid':e.data.uid
    }
    $api.post('member/login',data).then(res => {
      if(res.data.code == 1){
        wx.setStorageSync('uid',res.data.data.id);
        wx.setStorageSync('role',res.data.data.role);
        wx.setStorageSync('status',res.data.data.status);
        //手机号码不存在  弹出手机验证码页面
        e.setData({
          loginShow:false
        })
        if(!res.data.data.phone){
          e.setData({
            phoneShow:true,
          })
        }
      }
  }).catch(err => {
      //请求失败
      //console.log('error',err);
  })
}


//发送手机验证码
function sendCode(e){
  if(e.data.phone.length != 11){
    wx.showModal({
        title: "提示",
        showCancel: !1,
        content: "非法手机号码!!",
    })
    return;
  }
  var data = {phone:e.data.phone}
  $api.post('messageSend',data).then(res =>{
    //console.log(res)
    wx.showModal({
      title: "提示",
      showCancel: !1,
      content:res.data.msg,
    })
    if(res.data.code == 1){
      //wx.setStorageSync('wxCode',res.data.data);
     // wx.setStorageSync('truephone',e.data.phone);
      e.setData({
        wxCode:res.data.data,
        truephone:e.data.phone
      })

      timeclear(e);
    }
  }).catch(err => {
      //请求失败
      //console.log('error','请求失败');
  })
}

//定时器
function timeclear(e){
  e.setData({
    sendShow:false,
    time:60,
    sendTxt:'60s'
  })
  e.data.setInter = setInterval(function(){
    if(e.data.time == 0){
        e.setData({
            sendShow:true
        })
        clearInterval(e.data.setInter)
    }else{
        var numVal = e.data.time - 1;
        e.setData({
            sendTxt: numVal+'s',
            time:numVal
        });
    }
  }, 1000);
}

//更新手机号码
function phonesave(e){
  if(!e.data.phoneCode){
    wx.showModal({
      title: "提示",
      showCancel: !1,
      content: "请输入验证码!!",
    })
    return;
  }
  if(e.data.phoneCode != e.data.wxCode){
    wx.showModal({
      title: "提示",
      showCancel: !1,
      content: "验证码错误!!",
    })
    return;
  }
  var data ={
    'phone':e.data.truephone,
    'uid':wx.getStorageSync('uid')
  }
  $api.put('member/save',data).then(res => {
    if(res.data.code == 1){
      wx.showModal({
        title: "提示",
        showCancel: !1,
        content: "验证成功!!",
      })
      e.setData({
        phoneShow:false
      })
    }
  })
}

function jump(e){

}

function getCity(e){
  var data = {}
  $api.get('job/getCity',data).then(res => {
    if(res.data.code == 1){
      e.setData({
        citys:res.data.data
      })
    }
  })
}

function getTrain(e){
  var data = {}
  $api.get('job/getTrain',data).then(res => {
    if(res.data.code == 1){
      e.setData({
        train:res.data.data
      })
    }
  })
}

function getJobCate(e){
  var data = {}
  $api.get('job/list',data).then(res => {
      //console.log(res)
      //请求成功
      if(res.data.code == 1){
        e.setData({
          jobCate:res.data.data
        })
      }
  }).catch(err => {
      //请求失败
      //console.log('error','请求失败');
  })
}

function bindAllInput(e,r){
  var inputName = e.currentTarget.dataset.name;
  var form = r.data.form
  form[inputName] = e.detail.value
  r.setData({
    form:form
  })
  //console.log('11111',r.data.form)
}
function bindChange(e,r){
  var inputName = e.currentTarget.dataset.name;
  var value = e.detail.value;
  var key = e.currentTarget.dataset.key;
  var form = r.data.form
  form[inputName] = r.data[inputName][value]
  r.setData({
    form:form,
    [key]:value
  })
}

//遍历数组  返回提示数据
function message(data,form){
  for (var x in data){
    //console.log(form[x])
    if(!form[x]){
        return data[x];
        return;
    }
  }
  return false;
}

function checkstatus(){
  var data = {'uid':wx.getStorageSync('uid')}
  $api.get('member/status',data).then(res => {
    //console.log(res.data.data.status)
    if(res.data.code == 1){
      //console.log(res.data.data.status)
      return res.data.data.status
    }
  })
}

//转化成小程序模板语言 这一步非常重要 不然无法正确调用
module.exports = {
  login: login,
  addUser:addUser,
  sendCode:sendCode,
  phonesave:phonesave,
  checkphone:checkphone,
  getCity:getCity,
  getTrain:getTrain,
  getJobCate:getJobCate,
  bindAllInput:bindAllInput,
  bindChange:bindChange,
  message:message,
  checkstatus:checkstatus,
  wxLogin:wxLogin

};